module.exports = function (app, models, $, db, nm) {
	//获取用户列表
	app.get('/v1/user', (req, res) => {
		var query = {
			pageSize: req.query.pageSize || 10,
			pageNum: req.query.pageNum || 1,
			unitId: req.unitId
		}
		if (req.query.name) query.name = { $regex: req.query.name }
		if (req.query.nickName) query.nickName = { $regex: req.query.nickName }
		models.user.list(query, ret => {
			res.send(ret)
		}, {
			projection: {
				photo: 1,
				userName: 1,
				nickName: 1,
				name: 1,
				sex: 1,
				phone: 1,
				IDCard: 1,
				brisday: 1,
				address: 1,
				status: 1,
				_id: 'userId'
			}
		})
	})
	//新增用户信息
	app.post('/v1/user', (req, res) => {
		req.body.unitId = req.unitId
		models.user.insert(req.body, ret => {
			res.send(ret)
		})
	})
	//修改用户信息
	app.put('/v1/user', (req, res) => {
		req.body.userName = undefined
		models.user.update({ _id: req.body._id || req.body.userId }, req.body, ret => {
			res.send(ret)
		})
	})
	//获取用户详情
	app.get('/v1/userInfo', (req, res) => {
		if (req.query.userId) {
			models.user.find({ _id: req.query.userId }, ret => {
				res.send(ret)
			})
		} else {
			res.send({ errorMsg: '请传入userId' })
		}
	})
	//重置密码
	app.get('/v1/resetPassWord', (req, res) => {
		if (req.query.userId) {
			models.user.update({ _id: req.query.userId }, { passWord: 'e10adc3949ba59abbe56e057f20f883e' }, ret => {
				res.send(ret)
			})
		} else {
			res.send({ errorMsg: '请传入userId' })
		}
	})
	//修改密码
	// app.post('/v1/changePassWord', function (req, res) {
	// 	if (req.body.userName) {
	// 		models.user.find({
	// 			userName: req.body.userName
	// 		}, function (ret) {
	// 			if (ret.errorMsg) {
	// 				res.send(ret);
	// 			} else if (!ret._id) {
	// 				res.send({
	// 					errorMsg: '用户不存在!'
	// 				});
	// 			} else {
	// 				if (ret.passWord != req.body.passWord) {
	// 					res.send({
	// 						errorMsg: '旧密码输入错误!'
	// 					});
	// 				} else if (ret.passWord == req.body.newPassWord) {
	// 					res.send({
	// 						errorMsg: '新密码不能与旧密码一样!'
	// 					});
	// 				} else {
	// 					models.user.update({
	// 						_id: ret._id
	// 					}, {
	// 						passWord: req.body.newPassWord
	// 					}, function (ret) {
	// 						res.send(ret);
	// 					})
	// 				}
	// 			}
	// 		})
	// 	} else {
	// 		res.send({
	// 			errorMsg: '抱歉，您不能这样做！'
	// 		});
	// 	}
	// })
	//获取用户群组(权限)列表
	app.get('/v1/groupUser', (req, res) => {
		if(!req.query.userId) {
			res.send({errorMsg:'请传入userId'})
			return false
		}
		models.group_user.list({ userId: req.query.userId }, ret => {
			if(!ret.errorMsg){
				let groupUser = {}
				for(let i = 0; i < ret.length; i ++) groupUser[ret[i].groupId] = ret[i]._id;
				var query = {
					pageSize:req.query.pageSize || 10,
					pageNum:req.query.pageNum || 1,
					status:1,
					unitId:req.unitId
				}
				if(req.query.name) query.name = { $regex:req.query.name }
				models.group.list(query, ret => {
					if(ret.rows && ret.rows.length){
						for(let i = 0; i < ret.rows.length; i ++){
							ret.rows[i].status = groupUser[ret.rows[i]._id] ? 1 : 0
						}
					}
					res.send(ret)
				},{
					projection: {
						name: 1,
						_id:1,
						desoptions:1
					}
				})
			}else{
				res.send(ret)
			}
		})
	})
	//新增用户群组(权限)
	app.post('/v1/groupUser', (req, res) => {
		models.group_user.insert({ groupId: req.body.groupId, userId: req.body.userId, unitId: req.unitId }, ret => {
			res.send(ret)
		})
	})
	//删除用户群组(权限)
	app.delete('/v1/groupUser', (req, res) => {
		models.group_user.remove({ groupId:req.query.groupId,userId: req.query.userId,unitId:req.unitId }, ret => {
			res.send(ret)
		},{real:true})
	})
}